package com.bdqn.springsecurity1.controller;

import com.bdqn.springsecurity1.util.ValidCode;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.security.authentication.*;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.web.WebAttributes;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.awt.image.BufferedImage;
import java.io.IOException;

@Controller
public class SecurityController {
    //首页的访问路径
    @GetMapping("/index")
    public String index() {
        return "index";
    }

    //示例：访问menu1/1.将返回menu1/1.html
    @GetMapping("/menu{num}/{id}")
    public String menu(@PathVariable("num") int num, @PathVariable("id") int id) {
        return "menu" + num + "/" + id;
    }
    //自定义登录页的访问路径
    @GetMapping("/toLogin")
    public String tologin(HttpServletRequest request) {
        String msg = (String) request.getSession().getAttribute("msg");
        if (msg != null) {
            request.setAttribute("msg", msg);
        }
        return "login";
    }

    @GetMapping("/toLogin/error")
    public String toLogin(HttpServletRequest request, Model model) {
        AuthenticationException authenticationException = (AuthenticationException) request.getSession().getAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
        if (authenticationException instanceof UsernameNotFoundException || authenticationException instanceof BadCredentialsException) {
            model.addAttribute("msg", "用户名或密码错误");
        } else if (authenticationException instanceof DisabledException) {
            model.addAttribute("msg", "用户已被禁用");
        } else if (authenticationException instanceof LockedException) {
            model.addAttribute("msg", "账户被锁定");
        } else if (authenticationException instanceof AccountExpiredException) {
            model.addAttribute("msg", "账户过期");
        } else if (authenticationException instanceof CredentialsExpiredException) {
            model.addAttribute("msg", "证书过期");
        }
        return "login";
    }

    @GetMapping("/validcode")
    public void getValidPicture(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ValidCode validCode = new ValidCode();
        BufferedImage image = validCode.getImage();
        //获取随机验证码（字符串）
        String volidcode = validCode.getValidcode();
        System.out.println("volidcode = " + volidcode);
        HttpSession session = request.getSession();
        //将随机验证码存入session
        session.setAttribute("validcode", volidcode);
        //输出图片
        ValidCode.output(image, response.getOutputStream());
    }

    @GetMapping("/errorRole")
    public String errorRole() {
        return "errorRole";
    }
}
